/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zhuangfei.ohos_timetableview.dialog;

import com.zhuangfei.ohos_timetableview.ResourceTable;
import com.zhuangfei.ohos_timetableview.adapter.ChooseAdapter;
import com.zhuangfei.ohos_timetableview.bean.WeekChooseBean;
import com.zhuangfei.timetable.utils.ScreenUtils;

import ohos.agp.components.*;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;

import java.util.ArrayList;

/**
 * ChooseWeekDialog
 *
 * @since 2021-03-29
 */
public class ChooseWeekDialog {
    private final int target;
    private Context context;
    private String title = null;
    private ArrayList<WeekChooseBean> data = null;
    private CancleClickListen mCancleClickListen;
    private ConfimClickListen mConfimClickListen;
    private int mIndex;

    /**
     * 构造方法
     *
     * @param context
     * @param target
     */
    public ChooseWeekDialog(Context context, int target) {
        this.target = target;
        this.context = context;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setData(ArrayList<WeekChooseBean> data) {
        this.data = data;
    }

    public void setCancleButton(CancleClickListen cancleClickListen) {
        this.mCancleClickListen = cancleClickListen;
    }

    /**
     * 设置确认点击事件
     *
     * @param confimClickListen
     */
    public void setmConfimClickListen(ConfimClickListen confimClickListen) {
        this.mConfimClickListen = confimClickListen;
    }

    /**
     * 显示方法
     */
    public void show() {
        ComponentContainer customToastLayout = (ComponentContainer) LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_choose_dialog_layout, null, false);
        Text mTitle = (Text) customToastLayout.findComponentById(ResourceTable.Id_tv_title);
        ListContainer listContainer = (ListContainer) customToastLayout.findComponentById(ResourceTable.Id_lv_choose);
        Text mBtCancle = (Text) customToastLayout.findComponentById(ResourceTable.Id_bt_cancle);
        Text mBtConfim = (Text) customToastLayout.findComponentById(ResourceTable.Id_bt_confim);

        if (title != null) {
            mTitle.setText(title);
        }
        if (data != null) {
            setAdapter(listContainer);
        }

        DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(
                DirectionalLayout.LayoutConfig.MATCH_PARENT, ScreenUtils.getDisplayHeight(context) / 7 * 6);
        customToastLayout.setLayoutConfig(layoutConfig);


        CommonDialog toastDialog = new CommonDialog(context);
        toastDialog.setContentCustomComponent(customToastLayout);
        toastDialog.setAutoClosable(false);
        toastDialog.setSize(ScreenUtils.getDisplayWidth(context) / 6 * 5, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        toastDialog.setAlignment(LayoutAlignment.CENTER);
        toastDialog.show();
        mBtCancle.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                mCancleClickListen.onCancleClick();
                toastDialog.hide();
            }
        });
        mBtConfim.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                mConfimClickListen.onConfimClick(mIndex);
                toastDialog.hide();
            }
        });
    }

    private void setAdapter(ListContainer listContainer) {
        for (int i = 0; i < data.size(); i++) {
            if (i == target) {
                data.get(i).setSelect(true);
            } else {
                data.get(i).setSelect(false);
            }
        }
        ChooseAdapter adapter = new ChooseAdapter(context, data);
        adapter.setCheckListen(new ChooseAdapter.CheckListen() {
            @Override
            public void check(int index) {
                mIndex = index;
            }
        });
        listContainer.setItemProvider(adapter);
        listContainer.scrollTo(target);
        adapter.notifyDataChanged();
    }

    /**
     * CancleClickListen
     *
     * @since 2021-04-22
     */
    public interface CancleClickListen {
        /**
         * onCancleClick
         */
        void onCancleClick();
    }

    /**
     * ConfimClickListen
     *
     * @since 2021-04-22
     */
    public interface ConfimClickListen {
        /**
         * onCancleClick
         *
         * @param index
         */
        void onConfimClick(int index);
    }
}
